package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import layout.TableLayout;

import com.hexidec.util.Translatrix;

import eventHandling.EventBus;
import eventHandling.EventTyp.Type;

/**
 * The CreateNewLessonDialog is used to create a new Lesson.
 * 
 * @author sloth42
 * 
 */
public class CreateNewLessonDialog extends JDialog {

	private JButton btnCreate = new JButton(Translatrix
			.getTranslationString("create"));
	private JButton btnAbort = new JButton(Translatrix
			.getTranslationString("abort"));
	private JLabel lblDescription = new JLabel(Translatrix
			.getTranslationString("description"));
	private JLabel lblRepeatMode = new JLabel(Translatrix
			.getTranslationString("repeatMode"));
	private JLabel lblOnlyVocabulary = new JLabel(Translatrix
			.getTranslationString("onlyVocabulary"));
	private JTextArea taDescription = new JTextArea();
	// TODO maybe it would be better to force the repeating types to add their
	// name by themselves so you don't have to touch this class when adding a
	// new repeating type
	private String[] comboBoxList = new String[] {
			Translatrix.getTranslationString("repeatByTyping"),
			Translatrix.getTranslationString("repeatByShowing") };
	private JComboBox comboBoxRepeatMode = new JComboBox(comboBoxList);
	private JCheckBox checkBoxOnlyVocabulary = new JCheckBox();
	private JPanel pnlButtons = new JPanel();

	private EventBus eventBus;

	public CreateNewLessonDialog(EventBus eventBus, MainFrame mainFrame) {

		super(mainFrame);
		this.eventBus = eventBus;
		initButtonPanel();

		double border = 5;
		double size[][] = { { border, TableLayout.FILL, 20, border }, // Columns
				{ border, 20, 20, 20, 20, TableLayout.FILL, 30, border } }; // Rows
		this.setLayout(new TableLayout(size));

		// c r c r
		this.add(lblRepeatMode, "1,1");
		this.add(comboBoxRepeatMode, "1,2");
		this.add(lblOnlyVocabulary, "1,3");
		this.add(checkBoxOnlyVocabulary, "2,3");
		this.add(lblDescription, "1,4");
		this.add(taDescription, "1,5,2,5");
		this.add(pnlButtons, "1,6,2,6");

		this.setTitle(Translatrix.getTranslationString("createNewLesson"));
		this.setSize(300, 400);
	}

	private void initButtonPanel() {
		intiButtons();
		double border = 5;
		double size[][] = { { border, 0.5, 0.5, border }, // Columns
				{ border, 20, border } }; // Rows
		this.setLayout(new TableLayout(size));
		pnlButtons.setLayout(new TableLayout(size));
		pnlButtons.add(btnAbort, "1,1");
		pnlButtons.add(btnCreate, "2,1");
	}

	private void intiButtons() {
		btnAbort.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				eventBus.fireEvent(Type.CLOSE_CREATE_LESSON_DIALOG);

			}
		});

		btnCreate.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				eventBus.fireEvent(Type.CREATE_NEW_LESSON);
			}
		});
	}

	public String getDescription() {
		return taDescription.getText();
	}

	/**
	 * Get the selected index of the JComboBox to decide which repeating type is
	 * selected
	 * 
	 * @return
	 */
	public int getRepeatModeIndex() {
		return comboBoxRepeatMode.getSelectedIndex();
	}

	public boolean isOnlyVocabularySelected() {
		return checkBoxOnlyVocabulary.isSelected();
	}

}
